package com.szh.cloud.mybatis.flexigrid;

import com.szh.cloud.mybatis.annotation.Columns;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * flexigrid的数据
 */
public class FlexResEntity<T> {
    private Integer total;
    private Integer page;
    private List<FlexRowsType> rows =
            new ArrayList<FlexRowsType>();

    private List<Object> columns =
            new ArrayList<>();

    private List<T> rawRecords;

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public List<FlexRowsType> getRows() {
        return rows;
    }

    public void setRows(List<FlexRowsType> rows) {
        this.rows = rows;
    }

    public List<Object> getColumns() {
        return columns;
    }

    public void setColumns(List<Object> columns) {
        this.columns = columns;
    }

    public List<T> getRawRecords() {
        return rawRecords;
    }

    public void setRawRecords(List<T> rawRecords) {
        this.rawRecords = rawRecords;
    }

    public void deal(){
        if(rawRecords == null || rawRecords.size() == 0){
            return;
        }
        //列名称
        Class<?> classez = this.rawRecords.get(0).getClass();
        Field[] fields = classez.getDeclaredFields();
        for(Field field:fields){
            if(field.isAnnotationPresent(Columns.class)){
                this.columns.add(field.getName());
            }
        }//end for

        FlexRowsType flexRowsType;

        Object columnItem;
        Columns columns;
        T item;
        for(int i =0; i < this.rawRecords.size(); i ++){
            item = this.rawRecords.get(i);
            flexRowsType = new FlexRowsType(i);
            this.rows.add(flexRowsType);
            for(Field field:fields){
                if(field.isAnnotationPresent(Columns.class)){
                    field.setAccessible(true);
                    try {
                        columnItem = field.get(item);
                        flexRowsType.addCell(columnItem);
                    } catch (IllegalAccessException e) {

                    }
                }
            }//end for
        }//end for

    }
}
